package com.example.yaseademo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Button btn_streaming;
    private Button btn_player;
    public static final int PERMISSION_REQUEST_CODE = 1001;
    private Button btn_chat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        checkPermissionAndCommit();

    }

    private void checkPermissionAndCommit() {
        final String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
        if (!hasPermissions(permissions)) {
            ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
        }
    }

    private Boolean hasPermissions(String[] permissions) {
        for (String per : permissions) {
            if (ContextCompat.checkSelfPermission(getApplication(), per) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == PERMISSION_REQUEST_CODE) {
            String s = (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) ?
                    "权限被允许" : "权限被拒绝";
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        btn_streaming = (Button) findViewById(R.id.btn_streaming);
        btn_player = (Button) findViewById(R.id.btn_player);
        btn_streaming.setOnClickListener(this);
        btn_player.setOnClickListener(this);
        btn_chat = (Button) findViewById(R.id.btn_chat);
        btn_chat.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_streaming:
                startActivity(new Intent(this, StreamingActivity.class));
                break;
            case R.id.btn_player:
                startActivity(new Intent(this, PlayerActivity.class));
                break;
            case R.id.btn_chat:
                startActivity(new Intent(this, ChatActivity.class));
                break;
        }
    }
}
